import React, { FC, useReducer } from "react";

type actionType = { type: string; payload?: any };
type stateType = { msg: string };
const initialState: stateType = { msg: "初始数据" };

function msgReducer(state: stateType, action: actionType) {
  switch (action.type) {
    case "changemsg":
      state.msg = action.payload;
      return { ...state };
    default:
      return { ...state };
  }
}

const Reducertest: FC = () => {
  const [state, dispatch] = useReducer(msgReducer, initialState);
  return (
    <>
      <h1>测试usereducer ---{state.msg}</h1>
      <button
        onClick={() => {
          dispatch({
            type: "changemsg",
            payload: "usereducer修改数据",
          });
        }}
      >
        {" "}
        修改数据
      </button>
    </>
  );
};

export default Reducertest;
